<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['ruoyi-device:modelbuildgroup:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ruoyi-device:modelbuildgroup:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ruoyi-device:modelbuildgroup:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['ruoyi-device:modelbuildgroup:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="modelbuildgroupList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="所属单位" align="center" prop="deptName" />
      <el-table-column label="关联楼宇" align="center" prop="buildNameFirst" />
      <el-table-column label="名称" align="center" prop="name" />
      <!-- <el-table-column label="类型名称" align="center" prop="typeName" /> -->
      <el-table-column label="区域" align="center" prop="areaName" />
      <!-- <el-table-column label="场景FBX" align="center" prop="sceneName" /> -->
      <el-table-column label="类型" align="center">
        <template #default="scope">
          <div :style="scope.row.typeStyle">{{ scope.row.typeResult }}</div>
        </template>
      </el-table-column>
      <!-- <el-table-column label="建筑FBX" align="center" prop="buildName" /> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="250"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="handleToPage(scope.row)"
            v-hasPermi="['ruoyi-device:modelbuildgroup:edit']"
            ><el-icon><Place /></el-icon>编辑监控组</el-button
          >
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ruoyi-device:modelbuildgroup:edit']"
            >修改</el-button
          >
          <el-button
            link
            type="primary"
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ruoyi-device:modelbuildgroup:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改监控建筑分组对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form
        ref="modelbuildgroupRef"
        :model="form"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="建筑" prop="bId">
          <el-select v-model="form.bId" placeholder="请选择建筑" filterable>
            <el-option
              v-for="item in optionsBuilding"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属单位" prop="deptId">
          <el-tree-select
            v-model="form.deptId"
            :data="optionsDept"
            :props="{
              value: 'deptId',
              label: 'deptName',
              children: 'children',
            }"
            placeholder="请选择单位"
            check-strictly
          />
        </el-form-item>
        <el-form-item label="区域" prop="areaId">
          <el-select
            v-model="form.areaId"
            placeholder="请选择区域"
            filterable
          >
            <el-option
              v-for="item in optionsArea"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入类型名称" />
        </el-form-item>

        <el-form-item label="场景FBX" prop="sceneName">
          <el-input v-model="form.sceneName" placeholder="请输入场景FBX" />
        </el-form-item>
        <el-form-item label="类型" prop="typeId">
          <el-select v-model="form.typeId" placeholder="请选择" filterable>
            <el-option
              v-for="item in optionsType"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="建筑FBX" prop="buildName">
          <el-input v-model="form.buildName" placeholder="请输入建筑FBX" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Modelbuildgroup">
import { useRouter } from "vue-router";
const router = useRouter();
function handleToPage(entity1) {
  router.push({
    path: "modelfloorgroup",
    query: {
      id: entity1.id,
    },
  });
}

const optionsType = ref([
  {
    label: "楼宇",
    value: 1,
  },
  {
    label: "非楼宇",
    value: 2,
  },
]);

import { listArea } from "@/api/baseInfo/area";
const optionsArea = ref([]);
function getOptionsArea(tempList) {
  optionsArea.value = [];
  tempList.forEach((element) => {
    optionsArea.value.push({
      label: element.name,
      value: element.id,
    });
  });
}
const arrayArea = ref([]);
function getArrayArea() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 20000,
  };
  listArea(queryParams1).then((response) => {
    arrayArea.value = response.rows;
    getOptionsArea(response.rows);
  });
}

const optionsBuilding = ref([]);
function getOptionsBuilding(tempList) {
  optionsBuilding.value = [];
  tempList.forEach((element) => {
    optionsBuilding.value.push({
      label: element.name,
      value: element.id,
    });
  });
}

import { listBuilding } from "@/api/baseInfo/building";
const arrayBuilding = ref([]);
function getArrayBuilding() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 20000,
  };
  listBuilding(queryParams1).then((response) => {
    arrayBuilding.value = response.rows;
    getOptionsBuilding(response.rows);
  });
}

import { onMounted } from "vue";
onMounted(() => {
  getTreeselectDept();
  getArrayBuilding();
  getArrayArea();
});
import { listDept } from "@/api/system/dept";
const optionsDept = ref([]);
const arrayDept = ref([]);
function getTreeselectDept() {
  let queryParams1 = {
    pageNum: 1,
    pageSize: 10000,
  };
  listDept(queryParams1).then((response) => {
    optionsDept.value = [];
    arrayDept.value = response.data;
    arrayDept.value.forEach((item1) => {
      item1.deptId = item1.deptId * 1;
    });
    optionsDept.value = proxy.handleTree(arrayDept.value, "deptId");
  });
}

import {
  listModelbuildgroup,
  getModelbuildgroup,
  delModelbuildgroup,
  addModelbuildgroup,
  updateModelbuildgroup,
} from "@/api/baseInfo/modelbuildgroup";

const { proxy } = getCurrentInstance();

const modelbuildgroupList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    sceneName: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询监控建筑分组列表 */
function getList() {
  loading.value = true;
  listModelbuildgroup(queryParams.value).then((response) => {
    modelbuildgroupList.value = response.rows;
    total.value = response.total;
    loading.value = false;
    modelbuildgroupList.value.forEach((item1) => {
      if (item1.typeId == 1) {
        item1.typeResult = "楼宇";
        item1.typeStyle = {
          color: "green",
        };
      } else if (item1.typeId == 2) {
        item1.typeResult = "非楼宇";
        item1.typeStyle = {
          color: "red",
        };
      }
    });
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    bId: null,
    deptId: null,
    typeName: null,
    areaId: null,
    sceneName: null,
    typeId: null,
    buildName: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    createBy: null,
    remark: null,
    delFlag: null,
  };
  proxy.resetForm("modelbuildgroupRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加监控建筑分组";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getModelbuildgroup(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改监控建筑分组";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["modelbuildgroupRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateModelbuildgroup(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addModelbuildgroup(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除监控建筑分组编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delModelbuildgroup(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "ruoyi-device/modelbuildgroup/export",
    {
      ...queryParams.value,
    },
    `modelbuildgroup_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
